package cn.lyjuan.android.demofive.ui.slide.flipper;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import cn.lyjuan.android.demofive.R;

/**
 * Created by chad on 2016/3/16.
 */
public class FlipperActivity extends Activity implements GestureDetector.OnGestureListener
{
    private ViewFlipper flipper;
    private GestureDetector detector;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.setContentView(R.layout.ui_flipper);

        detector = new GestureDetector(this, this);
        flipper = (ViewFlipper) this.findViewById(R.id.ui_flipper);

        flipper.addView(addImageView(R.drawable.img_01));
        flipper.addView(addImageView(R.drawable.img_02));
        flipper.addView(addImageView(R.drawable.img_03));
        flipper.addView(addImageView(R.drawable.img_04));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        return this.detector.onTouchEvent(event);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if (e1.getX() - e2.getX() > 60)// 向左滑动
        {
            this.flipper.setInAnimation(loadAnimation(R.anim.ui_flipper_left_in));
            this.flipper.setOutAnimation(loadAnimation(R.anim.ui_flipper_left_out));
            this.flipper.showNext();
            return true;
        } else if (e1.getX() - e2.getX() < -60)// 向右滑动
        {
            this.flipper.setInAnimation(loadAnimation(R.anim.ui_flipper_right_in));
            this.flipper.setOutAnimation(loadAnimation(R.anim.ui_flipper_right_out));
            this.flipper.showPrevious();
            return true;
        }

        return false;
    }

    private Animation loadAnimation(int id)
    {
        return AnimationUtils.loadAnimation(this, id);
    }

    private ImageView addImageView(int id)
    {
        ImageView img = new ImageView(this);
        img.setImageResource(id);

        return img;
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {

    }


}
